using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WebLog.Db.SqlLogHelpers;

namespace WebLog
{
    public class PageModels
    {
        public static object Get(string tn)
        {
            Dictionary<string, List<Type>> keyValuePairs = new Dictionary<string, List<Type>>();
            keyValuePairs.Add("getSearchDto", new List<Type> { typeof(SearchLogInputDto) });
            keyValuePairs.Add("log", new List<Type> { typeof(WebReqLog) });
            keyValuePairs.Add("config", new List<Type> { typeof(ConfigInputDto) });
            keyValuePairs.Add("logPath", new List<Type> { typeof(SearchLogPathsInputDto) });//todo: 使用 SearchLogPathsInputDto 会产生bug
            keyValuePairs.Add("sysLog", new List<Type> { typeof(SysLogDto) });
            var prefix = "/WebLogIndex/Get/";//前缀
#if DEBUG
            prefix = "/WebLogIndex/Get/";
#endif
            return GetPageTagOptionsByDto(tn, keyValuePairs, prefix);
        }

        public static object GetPageTagOptionsByDto(string tn, Dictionary<string, List<Type>> keyValuePairs, string prefix = "")
        {
            if (keyValuePairs == null) throw Comm.TypeFactory.CreateBizException("调用错误");
            for (int i = 0, j = keyValuePairs.Keys.Count; i < j; i++)
            {
                if (keyValuePairs.Keys.ToArray()[i].ToLower() != keyValuePairs.Keys.ToArray()[i])
                {
                    keyValuePairs.Add(keyValuePairs.Keys.ToArray()[i].ToLower(), keyValuePairs.Values.ToArray()[i]);
                }
            }
            if (tn.IsNullEmpty())
            {
                var stackTrace = new System.Diagnostics.StackTrace();
                var pFunc = stackTrace.GetFrame(1).GetMethod();//上一级方法（调用当前方法的方法）
                                                               //前缀
                var str = prefix;//非调试模式由调用方添加头部 因为堆栈获取不到上一级方法 只能获取到顶级方法
                if (prefix.IsNullEmpty())
                {
                    str = "/" + new System.Text.RegularExpressions.Regex("Controller$").Replace(pFunc.DeclaringType.Name, "") +
                        "/" + pFunc.Name + "/";
                }

                var urls = (keyValuePairs.Keys.ToArray().Clone() as string[]);
                Dictionary<string, object> allUrlReturns = new Dictionary<string, object> { };
                for (int i = 0; i < urls.Length; i++)
                {
                    var url = urls[i].ToLower();
                    if (allUrlReturns.ContainsKey((str + url).ToLower())) continue;
                    var list = DtoToControlProps.DtoToDialogOb(keyValuePairs[url]);
                    url = str + url;
                    allUrlReturns.Add(url.ToLower(), list);
                }
                return allUrlReturns;
            }
            if (keyValuePairs.ContainsKey(tn.ToLower()))
            {
                return DtoToControlProps.DtoToDialogOb(keyValuePairs[tn]);
            }
            else throw Comm.TypeFactory.CreateBizException("未匹配到对应dto");
        }

        public static Dictionary<string, object> GetControlTypes()
        {
            Dictionary<string, object> results = new Dictionary<string, object> { };
            Action<Dictionary<string, object>> setResults = (val) =>
            {
                if (val != null)
                    foreach (var kv in (val as Dictionary<string, object>))
                    {
                        if (!results.ContainsKey(kv.Key))
                            results.Add(kv.Key, kv.Value);
                    }
            };
            setResults(PageModels.Get("") as Dictionary<string, object>);//得到所有路由及类型
            return results;
        }
    }
}